﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Uthorization.API.Application.Command.RoleCommand;
using RBAC_Uthorization.API.Application.Command.UserRoleCommand;
using RBAC_Uthorization.API.DTO;
using RBAC_Uthorization.API.DTO.Request;
using RBAC_Uthorization.API.DTO.Request.Role;
using RBAC_Uthorization.API.DTO.Request.UserRole;
using RBAC_Uthorization.Domain;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace RBAC_Uthorization.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<RoleController> _logger;

        public RoleController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> roleCreate(RoleCreateDto dto)
        {
            try
            {
                var result = await _mediator.Send(new RoleCreateCommand()
                {
                    dto = dto,
                    offic_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                    createName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                    sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
                });
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建角色服务器异常");
                return new ApiResult<int>() { Code = ResultCode.Fail, Message = "创建角色服务器异常" };
            }
        }
        /// <summary>
        /// 用户角色添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UserRoleCreate(batchCreateUserRoleDto dto)
        {
            try
            {
                var result = await _mediator.Send(new UserRoleCreateCommand()
                {
                    dto = dto,
                    createName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value
                });
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "新增用户角色服务器异常");
                return new ApiResult<int>() { Code = ResultCode.Fail, Message = "新增用户角色服务器异常" };
            }
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> RoleUpdate(RoleUpdateDto dto)
        {
            try
            {
                var result = await _mediator.Send(new RoleUpdateCommand()
                {
                    dto = dto,
                    updateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value
                });
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "修改角色服务器异常");
                return new ApiResult<int>() { Code = ResultCode.Fail, Message = "修改角色服务器异常" };
            }
        }
        /// <summary>
        /// 角色批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> RoleDelete(int[] id)
        {
            try
            {
                var result = await _mediator.Send(new RoleDeleteCommand()
                {
                    id = id,
                    Createby = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                    sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
                });
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除角色服务器异常");
                return new ApiResult<int>() { Code = ResultCode.Fail, Message = "删除角色服务器异常" };
            }
        }
        /// <summary>
        /// 用户角色修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UserRoleUpdate(UserRoleUpdateDto dto)
        {
            try
            {
                var result = await _mediator.Send(new UserRoleUpdateCommand()
                {
                    dto = dto,
                    updateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                    sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
                });
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "用户角色更新服务器异常");
                return new ApiResult<int>() { Code = ResultCode.Fail, Message = "用户角色更新服务器异常" };
            }
        }

    }
}
